package Dao.Admin.Imp;

import Dao.Admin.TitleManagementDao;
import Entity.Role.Student;
import Entity.TitleAndPaper.Title;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TitleManagementDaoImp implements TitleManagementDao {

    private PreparedStatement ps = null;
    private ResultSet rs = null;

    @Override
    public List<Student> getStuWithoutTea(Connection conn) {
        List<Student> students = new ArrayList<>();
        try {
            String sql = "select sno,password,name,sex,tel,email,collname,classes,role,bh,major_name,tno " +
                    "from stu_info where tno Is null ";
            ps = conn.prepareStatement(sql);
            /*ps.setString(1, null);*/
            rs = ps.executeQuery();
            while (rs.next()) {
                String sNO = rs.getString("sno");
                String password = rs.getString("password");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                String tel = rs.getString("tel");
                String email = rs.getString("email");
                String collName = rs.getString("collname");
                String classes = rs.getString("classes");
                String role = rs.getString("role");
                String bh = rs.getString("bh");
                String majorName = rs.getString("major_name");
                String tNO = rs.getString("tno");
                Student student = new Student(sNO, password, name, sex, tel, email, collName, classes, role, bh, majorName, tNO, null);
                students.add(student);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return students;
    }

    @Override
    public Integer teacherAssign(String tno, String sno, Connection conn) {
        int rsSum = 0;
        try {
            String sql = "update stu_info set tNO = ? where sNO = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, tno);
            ps.setString(2, sno);
            rsSum = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return rsSum;
    }

    @Override
    public List<Title> adminGetAllTitles(Connection conn) {
        List<Title> titles = new ArrayList<>();
        try {
            String sql = "select xt_id, title, intr, `from`, categoty, nature, addtime, no, state, college_name, major_name, opinoin, applly, is_pass,tname,tno,sname " +
                    " from title ";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Title title1 = this.setTitleByRs();
                titles.add(title1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return titles;
    }

    public Title setTitleByRs() throws SQLException {
        Integer xtId = rs.getInt("xt_id");
        String title = rs.getString("title");
        String intr = rs.getString("intr");
        String from = rs.getString("from");
        String categoty = rs.getString("categoty");
        String nature = rs.getString("nature");
        String addtime = rs.getString("addtime");
        String no = rs.getString("no");
        String state = rs.getString("state");
        String collegeName = rs.getString("college_name");
        String majorName = rs.getString("major_name");
        String opinoin = rs.getString("opinoin");
        String applly = rs.getString("applly");
        String isPass = rs.getString("is_pass");
        String tNo = rs.getString("tno");
        String tName = rs.getString("tname");
        String sname = rs.getString("sname");
        return new Title(xtId, title, intr, from, categoty, nature, addtime, no, state, collegeName, majorName, opinoin, applly, isPass, tName, tNo, sname);
    }
}
